import { Controller } from "../../mvc/core/Controller";
import { SPoint } from "../../../Message/Public/SPoint";
import { SEntityId } from "../../../Message/Public/SEntityId";
import { PlantFlowerBlessWindow } from "../view/plantFlower/PlantFlowerBlessWindow";
import { PlantLightBlessWindow } from "../view/plantFlower/PlantLightBlessWindow";
import { MyPlantFlowerView } from "../view/plantFlower/MyPlantFlowerView";
import { MyPlantLightView } from "../view/plantFlower/MyPlantLightView";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { EPublicCommand } from "../../../Message/Command/EPublicCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SPlayerPlantFlowerInfo } from "../../../Message/Game/SPlayerPlantFlowerInfo";
import { DataEvent } from "../events/DataEvent";
import { GameMapUtil } from "../scene/map/GameMapUtil";
import { SPlantFlowerInfo } from "../../../Message/Game/SPlantFlowerInfo";
import { Global } from "../../../com/gengine/global/Global";
import { BossConfig } from "../resource/BossConfig";
import { BossRule } from "../rules/BossRule";
import { MonsterPlayer } from "../scene/player/entity/MonsterPlayer";
import { PlayerEvent } from "../scene/events/PlayerEvent";
import { RolePlayer } from "../scene/player/entity/RolePlayer";
import { EntityUtil } from "../scene/layer/utils/EntityUtil";
import { ThingUtil } from "../scene/layer/utils/ThingUtil";
import { EntityInfo } from "../scene/player/info/EntityInfo";
import { GameProxy } from "../mvc/GameProxy";
import { EPlantFlowerOperType } from "../../../Message/Game/EPlantFlowerOperType";
import { ItemData } from "../resource/info/ItemData";
import { Language } from "../../../extend/language/Language";
import { EProp } from "../../../Message/Public/EProp";
import { Alert } from "../../../com/mui/controls/Alert";
type int = number;
//class PlantFlowerController
    
    export  class PlantFlowerController extends Controller
    {
        constructor()
        {
            
            super();this._tpoint = new SPoint();
            this._tentityid = new SEntityId();
            return;
        }

        public get flowerBlessWindow(): PlantFlowerBlessWindow
        {
            if (!this._flowerBlessWindow) 
            {
                this._flowerBlessWindow = new PlantFlowerBlessWindow();
            }
            return this._flowerBlessWindow;
        }

        public get lightBlessWindow(): PlantLightBlessWindow
        {
            if (!this._lightBlessWindow) 
            {
                this._lightBlessWindow = new PlantLightBlessWindow();
            }
            return this._lightBlessWindow;
        }

        public get myFlowerView(): MyPlantFlowerView
        {
            if (!this._myFlowerView) 
            {
                this._myFlowerView = new MyPlantFlowerView();
            }
            return this._myFlowerView;
        }

        public get myLightView(): MyPlantLightView
        {
            if (!this._myLightView) 
            {
                this._myLightView = new MyPlantLightView();
            }
            return this._myLightView;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicPlayerPlantFlowerInfo, this.onReceivePlantFlowerInfoHandler);
            Dispatcher.addEventListener(EventName.PlantFlower_OpenFlowerBless, this.onOpenPlantFlowerBlessWindow);
            Dispatcher.addEventListener(EventName.PlantFlower_CloseFlowerBless, this.onClosePlantFlowerBlessWindow);
            Dispatcher.addEventListener(EventName.PlantFlower_UpdateFlowerBless, this.onUpdatePlantFlowerBlessWindow);
            Dispatcher.addEventListener(EventName.PlantFlower_InviteBless, this.doInviteBlessHandler);
            Dispatcher.addEventListener(EventName.PlantFlower_GetFlower, this.doGetFlowerHandler);
            Dispatcher.addEventListener(EventName.PlantFlower_DoBless, this.doBlessHandler);
            Dispatcher.addEventListener(EventName.PlantFlower_PlantFlower, this.doPlantFlowerHandler);
            Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpdateHandler);
            return;
        }

        /* internal  */onReceivePlantFlowerInfoHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SPlayerPlantFlowerInfo;
            cache.plantFlower.flowerInfo = loc1;
            this.updateMyFlowerInfo();
            return;
        }

        /* internal  */onSceneUpdateHandler(arg1: DataEvent): void
        {
            this.updateMyFlowerInfo();
            return;
        }

        public testMyflowerView(): void
        {
            var loc1=null;
            if (GameMapUtil.curMapState.isTaijiCity) 
            {
                this.myFlowerView.show();
                loc1 = new SPlantFlowerInfo();
                loc1.bossId = 3103000;
                loc1.hadBlessPlayers = [11, 22, 33];
                loc1.point = new SPoint();
                loc1.point.x = 36;
                loc1.point.y = 210;
                this.myFlowerView.updateMyFlowerState(loc1);
                Global.stage.addEventListener(flash.events.Event.RESIZE, this.onStageResizeHandler);
                this.onStageResizeHandler();
            }
            else 
            {
                if (this._myFlowerView && !this._myFlowerView.isHide) 
                {
                    this._myFlowerView.hide();
                }
                Global.stage.removeEventListener(flash.events.Event.RESIZE, this.onStageResizeHandler);
            }
            return;
        }

        /* internal  */updateMyFlowerInfo(): void
        {
            var loc1=null;
            var loc2=null;
            if (GameMapUtil.curMapState.isTaijiCity && cache.plantFlower.flowerInfo && cache.plantFlower.flowerInfo.playerPlantFlowerInfo.length > 0) 
            {
                loc1 = cache.plantFlower.myPlantFlowerInfo;
                loc2 = BossConfig.instance.getInfoByName(loc1.bossId);
                if (BossRule.isPlantBossLight(loc2)) 
                {
                    this.myLightView.show();
                    this.myLightView.updateMyLightState(loc1);
                }
                else 
                {
                    this.myFlowerView.show();
                    this.myFlowerView.updateMyFlowerState(loc1);
                }
                Global.stage.addEventListener(flash.events.Event.RESIZE, this.onStageResizeHandler);
                this.onStageResizeHandler();
            }
            else 
            {
                if (this._myFlowerView && !this._myFlowerView.isHide) 
                {
                    this._myFlowerView.hide();
                }
                if (this._myLightView && !this._myLightView.isHide) 
                {
                    this._myLightView.hide();
                }
                Global.stage.removeEventListener(flash.events.Event.RESIZE, this.onStageResizeHandler);
            }
            return;
        }

        /* internal  */onStageResizeHandler(arg1: Object /* flash.events.Event */=null): void
        {
            if (this._myFlowerView) 
            {
                this._myFlowerView.x = Global.stage.stageWidth - 438;
                this._myFlowerView.y = Global.stage.stageHeight - 195;
            }
            if (this._myLightView) 
            {
                this._myLightView.x = Global.stage.stageWidth - 438;
                this._myLightView.y = Global.stage.stageHeight - 195;
            }
            return;
        }

        /* internal  */onOpenPlantFlowerBlessWindow(arg1: DataEvent): void
        {
            this._selectedPlant = arg1.data as MonsterPlayer;
            if (this._selectedPlant) 
            {
                if (BossRule.isPlantBossLight(this._selectedPlant.bossInfo.boss)) 
                {
                    this.lightBlessWindow.updateFlowerInfo(this._selectedPlant.entityInfo, this._selectedPlant.bossInfo.boss);
                    this.lightBlessWindow.show();
                }
                else 
                {
                    this.flowerBlessWindow.updateFlowerInfo(this._selectedPlant.entityInfo, this._selectedPlant.bossInfo.boss);
                    this.flowerBlessWindow.show();
                }
                this._selectedPlant.addEventListener(PlayerEvent.PlayerManaUpdate, this.onPlantManaUpdateHandler);
                RolePlayer.instance.addEventListener(PlayerEvent.GIRD_WALK_END, this.checkOutDistance);
                Dispatcher.addEventListener(EventName.Convey_Res, this.checkOutDistance);
                Dispatcher.addEventListener(EventName.ChangeScene, this.checkOutDistance);
            }
            return;
        }

        /* internal  */onClosePlantFlowerBlessWindow(arg1: DataEvent): void
        {
            var loc1=null;
            if (arg1.data) 
            {
                loc1 = arg1.data as SEntityId;
                if (this._selectedPlant && EntityUtil.equal(loc1, this._selectedPlant.entityInfo.entityInfo.entityId)) 
                {
                    this.hideBlessWindow();
                }
            }
            else 
            {
                this.hideBlessWindow();
            }
            return;
        }

        /* internal  */onUpdatePlantFlowerBlessWindow(arg1: DataEvent): void
        {
            this.updateBlessWindow();
            return;
        }

        /* internal  */onPlantManaUpdateHandler(arg1): void
        {
            this.updateBlessWindow();
            return;
        }

        /* internal  */updateBlessWindow(): void
        {
            if (this._selectedPlant) 
            {
                if (BossRule.isPlantBossLight(this._selectedPlant.bossInfo.boss)) 
                {
                    if (this._lightBlessWindow && !this._lightBlessWindow.isHide) 
                    {
                        this.lightBlessWindow.updateFlowerInfo(this._selectedPlant.entityInfo, this._selectedPlant.bossInfo.boss);
                    }
                }
                else if (this._flowerBlessWindow && !this._flowerBlessWindow.isHide) 
                {
                    this.flowerBlessWindow.updateFlowerInfo(this._selectedPlant.entityInfo, this._selectedPlant.bossInfo.boss);
                }
            }
            return;
        }

        /* internal  */checkOutDistance(arg1=null): void
        {
            if (this._selectedPlant && ThingUtil.entityUtil.getDistanceByTile(this._selectedPlant) > 120) 
            {
                this.hideBlessWindow();
            }
            return;
        }

        /* internal  */hideBlessWindow(): void
        {
            if (this._flowerBlessWindow && !this._flowerBlessWindow.isHide) 
            {
                this._flowerBlessWindow.hide();
            }
            if (this._lightBlessWindow && !this._lightBlessWindow.isHide) 
            {
                this._lightBlessWindow.hide();
            }
            if (this._selectedPlant) 
            {
                this._selectedPlant.removeEventListener(PlayerEvent.PlayerManaUpdate, this.onPlantManaUpdateHandler);
                this._selectedPlant = null;
            }
            RolePlayer.instance.removeEventListener(PlayerEvent.GIRD_WALK_END, this.checkOutDistance);
            Dispatcher.removeEventListener(EventName.Convey_Res, this.checkOutDistance);
            Dispatcher.removeEventListener(EventName.ChangeScene, this.checkOutDistance);
            return;
        }

        /* internal  */doInviteBlessHandler(arg1: DataEvent): void
        {
            var loc2=null;
            var loc1=arg1.data as EntityInfo;
            if (loc1 && loc1.entityInfo && this._selectedPlant) 
            {
                loc2 = new SPoint();
                loc2.x = this._selectedPlant.currentPoint.x;
                loc2.y = this._selectedPlant.currentPoint.y;
                GameProxy.plantFlower.plantFlowerOper(EPlantFlowerOperType.convert(EPlantFlowerOperType._EPlantFlowerOperInvite), "", loc1.entityInfo.code, loc2, loc1.entityInfo.entityId);
            }
            return;
        }

        /* internal  */doGetFlowerHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as EntityInfo;
            if (loc1 && loc1.entityInfo) 
            {
                GameProxy.plantFlower.plantFlowerOper(EPlantFlowerOperType.convert(EPlantFlowerOperType._EPlantFlowerOperGet), "", loc1.entityInfo.code, this._tpoint, loc1.entityInfo.entityId);
            }
            return;
        }

        /* internal  */doBlessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as EntityInfo;
            if (loc1 && loc1.entityInfo) 
            {
                GameProxy.plantFlower.plantFlowerOper(EPlantFlowerOperType.convert(EPlantFlowerOperType._EPlantFlowerOperBless), "", loc1.entityInfo.code, this._tpoint, loc1.entityInfo.entityId);
            }
            return;
        }

        /* internal  */doPlantFlowerHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as ItemData;
            var loc2=Language.getString(69819);
            if (loc1.type == EProp._EPropBurnLight) 
            {
                loc2 = Language.getString(69830);
            }
            Alert.extendObj = loc1;
            Alert.show(loc2, null, Alert.OK | Alert.CANCEL, null, this.plantFlowerAlertHandler);
            return;
        }

        /* internal  */plantFlowerAlertHandler(arg1: int, arg2: Object): void
        {
            var loc1=null;
            var loc2=null;
            if (arg1 == Alert.OK) 
            {
                loc1 = new SPoint();
                loc1.x = RolePlayer.instance.currentPoint.x;
                loc1.y = RolePlayer.instance.currentPoint.y;
                loc2 = arg2 as ItemData;
                GameProxy.plantFlower.plantFlowerOper(EPlantFlowerOperType.convert(EPlantFlowerOperType._EPlantFlowerOperPlant), loc2.uid, 0, loc1, this._tentityid);
            }
            return;
        }

        private /* var */_flowerBlessWindow: PlantFlowerBlessWindow;

        private /* var */_lightBlessWindow: PlantLightBlessWindow;

        private /* var */_myFlowerView: MyPlantFlowerView;

        private /* var */_myLightView: MyPlantLightView;

        private /* var */_selectedPlant: MonsterPlayer;

        private /* var */_tpoint: SPoint;

        private /* var */_tentityid: SEntityId;
    }
